package api;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * @author fangkun
 * @create 2022-11-25-11:15
 */
public class ArrayListDemo {
    public static void main(String[] args) {

        ArrayList<String> list = new ArrayList<>();

        //添加
        list.add("hello");
        list.add("world");
        list.add("hahaha");
        list.add("heiheihei");
        System.out.println(list);

        //获取,索引从0开始
        String s = list.get(1);
        System.out.println(s);

        //修改,可以返回修改前的元素
        list.set(1, "WORLD");
        System.out.println(list);

        //删除
        list.remove(2);
        System.out.println(list);

        //大小
        int size = list.size();
        System.out.println(size);

        //迭代
        for (int i = 0; i < list.size(); i++) {
            System.out.println(i + "..."+list.get(i));
        }

        for (String l : list){
            System.out.println("..."+l);
        }

        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        System.out.println("-----------------------");

        Collections.sort(list);
        for (String l : list){
            System.out.println("..."+l);
        }

        //forEach
        list.forEach((e) -> {
            e = "My" + e;
            System.out.println(e);
        });


    }
}
